package com.doge.calcalbum.business.sysalbum

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.doge.calcalbum.R
import com.doge.calcalbum.business.ITEM_CHECK
import com.doge.calcalbum.core.base.adapters.AdapterCallback
import com.doge.calcalbum.core.base.adapters.ITEM_CLICKED
import com.doge.calcalbum.core.base.adapters.ITEM_LONG_CLICKED
import com.doge.calcalbum.databinding.CellEntryBinding
import com.doge.calcalbum.utils.load
import com.doge.calcalbum.utils.setOnClickCallback

class SysEntryViewHolder(
    parent: ViewGroup,
    callback: AdapterCallback,
    val binding: CellEntryBinding = CellEntryBinding.inflate(
        LayoutInflater.from(parent.context),
        parent,
        false
    ),
) : RecyclerView.ViewHolder(binding.root) {


    init {
        binding.item.setOnClickCallback {
            callback.invoke(ITEM_CLICKED, itemView.tag)
        }
        binding.item.setOnLongClickListener {
            callback.invoke(ITEM_LONG_CLICKED, itemView.tag)
            true
        }
        binding.checkbox.setOnClickListener {
            callback.invoke(ITEM_CHECK, itemView.tag)
        }
    }

    fun bind(item: SysEntry) {
        itemView.tag = item
        Glide.with(binding.cover).load(item.filePath)
            .thumbnail(0.25f)
            .apply(RequestOptions().placeholder(binding.cover.drawable))
            .into(binding.cover)

        binding.cover.load(item.filePath)
        binding.checkbox.isVisible = true
        binding.checkbox.tag = item.selected
        binding.checkbox.setImageResource(
            if (item.selected) R.mipmap.check_selected else R.mipmap.check_normal
        )
        binding.checkboxShadow.isVisible = item.selected
    }
}